local youjian = fk.CreateSkill {
  name = "xiaobai__youjian",
}

Fk:loadTranslationTable{
  ["xiaobai__youjian"] = "忧谏",
  [":xiaobai__youjian"] = "出牌阶段限一次，你可以观看一名其他角色所有手牌并用至多两张手牌交换其中等量的牌。你于本回合使用以此获得的伤害牌后，可与其各摸一张牌。",
  ["#youjian-use"] = "忧谏：你可以与 %dest 各摸一张牌各摸一张牌",
  ["@@youjian"] = "忧谏",
  ["@@youjian-turn"] = "忧谏",
  ["#yongjian-exchange"] = "忧谏：交换至多两张牌",
}

youjian:addEffect("active", {
  anim_type = "support",
  target_num = 1,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards, card, extra_data)
    local target = to_select
    return #selected == 0 and target:getHandcardNum() > 0 and to_select ~= player
  end,
  can_use = function(self, player)
    return player:hasSkill(self) and player:getHandcardNum() > 0 and player:usedSkillTimes(youjian.name) == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local self_card = player:getCardIds("h")
    local target_card = target:getCardIds("h")
    local chooseCards = room:askToPoxi(player, {
      poxi_type = "xiaobai__youjian",
      data = {{player.general, target_card}, {target.general, self_card}},
      cancelable = false, 
      skill_name = youjian.name})
    if chooseCards == nil then
      return false
    end
    for _, value in ipairs(room:getAlivePlayers()) do
      if value:getMark("youjian") ~= 0 then
        room:setPlayerMark(value, "youjian", 0)
      end
    end
    room:setPlayerMark(target, "youjian", 1)
    local trans1 = table.filter(self_card, function(element, index, array)
      return table.contains(chooseCards, element)
    end)
    local trans2 = table.filter(target_card, function(element, index, array)
      return table.contains(chooseCards, element)
    end)
    room:swapCards(player, {{player,trans1}, {target,trans2}}, youjian.name)
    room:addPlayerMark(target, "@@youjian-turn")
    table.forEach(trans2, function(element, index, array)
      room:addCardMark(Fk:getCardById(element), "@@youjian-turn")
    end)
  end
})

youjian:addEffect(fk.CardUseFinished, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if not (player:hasSkill(self) and player:hasSkill(youjian.name) and data.card.is_damage_card) then return false end
    return #table.filter(Card:getIdList(data.card), function(id)
      return Fk:getCardById(id):getMark("@@youjian-turn") > 0
    end) > 0
  end,
  on_trigger = function(self, event, target, player, data)
    local num = #table.filter(Card:getIdList(data.card), function(id)
      return Fk:getCardById(id):getMark("@@youjian-turn") > 0
    end)
    for _ = 1, num, 1 do
      local tos = table.filter(player.room:getOtherPlayers(player), function(element, index, array)
        return element:getMark("@@youjian-turn") > 0
      end)
      for index, value in ipairs(tos) do
        self:doCost(event, value, player, data)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player,{skill_name = youjian.name,prompt = "#youjian-use::" .. target.id})
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, youjian.name)
    target:drawCards(1, youjian.name)
  end
})

Fk:addPoxiMethod{
  name = "xiaobai__youjian",
  card_filter = function(to_select, selected, data)
    if #selected == 4 then return end
    local first = 0
    local second = 0
    for _, cid in ipairs(selected) do
      if table.contains(data[1][2], cid) then
        first = first + 1
      else
        second = second + 1
      end
    end
    if first < second then
      return table.contains(data[1][2], to_select)
    elseif first > second then
      return table.contains(data[2][2], to_select)
    else
      return true
    end
  end,
  feasible = function (selected, data, extra_data)
    local first = 0
    local second = 0
    for _, cid in ipairs(selected) do
      if table.contains(data[1][2], cid) then
        first = first + 1
      else
        second = second + 1
      end
    end
    return first == second
  end,
} 

return youjian